#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int maxn = 10005;
int T;
int dfn[maxn],low[maxn],n,m;
int head[maxn],num_edge;
int scc,id[maxn];
bool vis[maxn];

struct node{
    int next;
    int to;
}edge[50005];

stack<int> st;

void add_edge(int from,int to){
    num_edge++;
    edge[num_edge].next=head[from];
    edge[num_edge].to=to;
    head[from]=num_edge;
}

void tarjan(int s){
    dfn[s]=low[s]=++T;
    st.push(s);
    vis[s]=true;
    for(int i=head[s];i!=0;i=edge[i].next){
        int to=edge[i].to;
        if(!dfn[to]){
            tarjan(to);
            low[s]=min(low[to],low[s]);
        }else if(vis[to]){
            low[s]=min(low[s],dfn[to]);
        }
    }
    if(low[s]==dfn[s]){
		scc++;
		int u;
		do{
			u=st.top();
			vis[u]=false;
			st.pop();
			id[u]=scc;
		}while(u!=s);
	}
    return;
}

int main(){
    cin>>n>>m;
    int a,b;
    for(int i=1;i<=m;i++){
        cin>>a>>b;
        add_edge(a,b);
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i])tarjan(i);
    }
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++){
        for(int k=head[i];k!=0;k=edge[k].next){
            if(id[i]!=id[edge[k].to]){
                vis[id[i]]=true;
            }
        }
    }
    int p,cnt=0;
    for(int i=1;i<=scc;i++){
        if(!vis[i]){
            cnt++;
            p=i;
        }
    }
    if(cnt>1)cout<<0<<endl;
    else{
        cnt=0;
        for(int i=1;i<=n;i++){
            if(id[i]==p)cnt++;
        }
        cout<<cnt<<endl;
    }
    
}
